package mvc.views.frames
{
	import alternativa.gui.container.Container;
	import alternativa.gui.container.gridContainer.GridContainer;
	import alternativa.gui.container.linear.VBox;
	import alternativa.gui.enum.Align;
	import alternativa.gui.theme.defaulttheme.container.panel.Panel;
	import alternativa.gui.theme.defaulttheme.controls.buttons.Button;
	import alternativa.gui.theme.defaulttheme.controls.buttons.CheckBox;
	import alternativa.gui.theme.defaulttheme.controls.text.Label;
	import alternativa.gui.theme.defaulttheme.controls.text.TextInput;
	import alternativa.init.GUI;
	
	import com.greensock.TweenLite;
	import com.greensock.TweenMax;
	import com.greensock.easing.Back;
	import com.greensock.easing.Cubic;
	import com.greensock.plugins.MotionBlurPlugin;
	import com.greensock.plugins.TweenPlugin;
	
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.ui.Keyboard;
	
	import globals.Config;
	import globals.Global;
	import globals.Restriction;
	import globals.Url;
	
	import interfaces.ILayoutAnimationFrame;
	
	import mvc.AppFacade;
	import mvc.Notifications;
	
	import utils.UtilsAnimation;
	import utils.UtilsUIManager;
	import utils.UtilsWeb;
	
	public class FrameLogin extends Frame implements ILayoutAnimationFrame
	{
		private var _holderPanel:Panel;
		
		private var _content:Container;
		private var _lUserName:Label;
		private var _lPassword:Label;
		private var _tiUserName:TextInput;
		private var _tiPassword:TextInput;
		private var _lError:Label;
		private var _cbRemember:CheckBox;
		private var _btnOk:Button;
		private var _btnRegister:Button;
		
		public function FrameLogin()
		{
			super();
		}
		
		public function get tiUserName():TextInput
		{
			return _tiUserName;
		}

		public function get tiPassword():TextInput
		{
			return _tiPassword;
		}

		public function get animationWidth():int
		{
			return 200;
		}
		
		public function get animationHeight():int
		{
			return 200;
		}
		
		override public function show():void
		{
			// TODO Auto Generated method stub
			super.show();
			UtilsUIManager.changeFocusOn(_tiUserName.tf);
			UtilsAnimation.showFrameStyle1(_holderPanel);
		}
		
		public function loginSuccess():void
		{
		}
		
		public function loginError($data:Object):void
		{
			for(var k:String in $data)
			{
				if($data[k] && this.hasOwnProperty(k))
				{
					UtilsUIManager.enableErrorByStyle1(this[k],_lError,$data[k]);
					return;					
				}

			}
			
		}
		
		override protected function initUI():void
		{
			// TODO Auto Generated method stub
			super.initUI();
			
			_holderPanel = new Panel();
			addChild(_holderPanel);
			
			_holderPanel.title = "登陆框";
			
			_content = new Container();
			_lUserName = UtilsUIManager.createLabel(_content,"登录名",15,10);
			_lPassword = UtilsUIManager.createLabel(_content,"密码",25,40);
			_tiUserName = UtilsUIManager.createTextInput(_content,60,5,100,30,"",Restriction.MAX_USER_NAME);
			_tiPassword = UtilsUIManager.createTextInput(_content,60,35,100,30,"",Restriction.MAX_PASSWORD);
			_lError = UtilsUIManager.createLabel(_content,"",0,70,180,30,Align.CENTER,0xff0000);
			_cbRemember = UtilsUIManager.createCheckBox(_content,"1个月内保持登陆",0,100);
			_btnOk = UtilsUIManager.createButton(_content,"登录",20,130,60,20);
			_btnRegister = UtilsUIManager.createButton(_content,"注册",100,130,60,20);
			
			_btnOk.addEventListener(MouseEvent.CLICK,onBtnClick);
			_btnRegister.addEventListener(MouseEvent.CLICK,onBtnClick);

			_tiUserName.addEventListener(KeyboardEvent.KEY_UP,onKeyUp);
			_tiPassword.addEventListener(KeyboardEvent.KEY_UP,onKeyUp);
			
			_tiPassword.displayAsPassword = true;
			
			_holderPanel.content = _content;
			
			_holderPanel.width = 200;
			_holderPanel.height = 200;
			_holderPanel.freezeHeight=true;
			_holderPanel.freezeWidth=true;
			
			resize();
		}
		
		private function onBtnClick(evt:MouseEvent):void
		{
			switch(evt.target)
			{
				case _btnOk:
					toLogin();
					break;
				case _btnRegister:
					UtilsWeb.openIFrame(Url.UEditor);
					break;
			}
		}
		
		private function onKeyUp(evt:KeyboardEvent):void
		{
			switch(evt.keyCode)
			{
				case Keyboard.ENTER:
					toLogin();
					break;
			}
		}
		
		private function toLogin():void
		{
			if(Config.isOffline)
			{
				AppFacade.getInstance().sendNotification(Notifications.MEDIATOR_GOTO_CLIENT_URL,Url.USER_INFO);
				return;
			}
			var username:String = _tiUserName.text;
			var password:String = _tiPassword.text;
			
			if(username.length>0)
			{
				UtilsUIManager.enableErrorByStyle1(_tiUserName,_lError,"",false);
				if(password.length>0)
				{
					UtilsUIManager.enableErrorByStyle1(_tiPassword,_lError,"",false);
					AppFacade.getInstance().sendNotification(Notifications.UI_TO_LOGIN,{username:username,password:password});
				}
				else
				{
					UtilsUIManager.enableErrorByStyle1(_tiPassword,_lError,"请输入密码");
				}
			}
			else
			{
				UtilsUIManager.enableErrorByStyle1(_tiUserName,_lError,"请输入用户名");
			}
		}
		
		override public function resize(evt:Event=null):void
		{
			// TODO Auto Generated method stub
			super.resize(evt);
			x = (Global.stage.stageWidth-width)/2;
			y = (Global.stage.stageHeight-height)/2;
		}
		
		public function get positionX():int
		{
			// TODO Auto Generated method stub
			return 0;
		}
		
		public function get positionY():int
		{
			// TODO Auto Generated method stub
			return 0;
		}
		
		public function endAnimation():void
		{
			// TODO Auto Generated method stub
			
		}
		
		public function startAnimation():void
		{
			 lock();
		}

		public function get holderPanel():Panel
		{
			return _holderPanel;
		}
		
	}
}